<template>
  <div class="personal-homepage">
    <!-- Header Cover Image -->
    <div class="header-cover" @click="triggerCoverUpload">
      <input type="file" id="cover" @change="uploadCover" style="display: none;" />
      <img :src="coverUrl" alt="Cover Image" />
    </div>
    
    <div style="display: flex;">
      <!-- Avatar Upload -->
      <div class="avatar-upload" @click="triggerAvatarUpload">
        <input type="file" id="avatar" @change="uploadAvatar" style="display: none;" />
        <img :src="userProfile.avatar" alt="Avatar"/>
      </div>
      <div class="userProfile">
        <div>
          <div class="name">{{ userProfile.nickname }}</div>
          <div><span class="number">{{ userProfile.count || 0 }}</span> 个作品</div>
        </div>
        <el-button @click="go_updateUserProfile" class="update_btn" round size="large">编辑资料</el-button>
      </div>
    </div>
    <div class="title">作品列表</div>
    <myVideoList :videoList="videoList" :avatar="userProfile.avatar" :username="userProfile.nickname"/>
    <div class="title">我的勋章</div>
    <div class="achievements">
      <div v-for="medal in userProfile.medalList" :key="medal.id" class="medal">
        <img :src="medal.icon" :alt="medal.name">
        <div>{{ medal.name }}</div>
        <div>{{ medal.updateAt }}</div>
        <div>{{ medal.remark }}</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

import { useLoginStore } from '../api/store'; 
import { myVideos } from '../api/pcApi'
import myVideoList from '../components/video-list.vue';

const router = useRouter()

const coverUrl = ref();

const go_updateUserProfile = () => {
  router.push('/myProfile')
};

// 用户资料的响应式状态
const userProfile = useLoginStore().userinfo;

console.log(userProfile);

const videoList = ref([])
const fetchVideoList = async () => {
  try {
    const res = await myVideos()
    videoList.value = res.rows
  } catch (error) {
    console.error('加载用户数据失败', error)
  }
}

// 生命周期钩子：获取用户数据
onMounted(() => {
  coverUrl.value = userProfile.cover || 'https://cn.bing.com/th?id=OHR.WhiteSandsNP_ZH-CN2517618394_1920x1080.webp&qlt=50'
  fetchVideoList()
})
</script>

<style lang="less" scoped>
.personal-homepage {
  width: 1160px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding-bottom: 180px;

  .header-cover {
    width: 100%;
    cursor: pointer;
    img {
      width: 100%;
      height: 220px;
      object-fit: cover;
    }
  }

  .avatar-upload {
    text-align: left;
    margin-left: 20px;
    margin-top: -40px;
    img {
      width: 155px;
      height: 155px;
      border-radius: 50%;
      border: 12px solid #F3F3F3;
      cursor: pointer;
    }
  }
  .userProfile {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-top: 20px;
    margin-left: 8px;
    .name {
      font-size: 22px;
      font-weight: 700;
      color: #A30000;
    }
    .number {
      color: #A30000;
      margin-right: 3px;
    }
    .update_btn {
      border-color: #A30000;
      color: #A30000;
      width: 168px;
      &:hover {
        background-color: #A30000;
        color: white;
      }
    }
  }
  .title {
    font-size: 24px;
    color: #A30000;
    font-weight: 700;
    margin-top: 40px;
    margin-bottom: 32px;
  }
  .achievements {
    display: flex;
    gap: 25px;
    .medal {
      background: #F4F4F6;
      border-radius: 16px;
      padding: 40px 25px;
      text-align: center;
      img {
        width: 180px;
        height: 180px;
      }
    }
  }
}





.medal img {
  width: 50px;
  height: 50px;
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

.video-card {
  cursor: pointer;
  transition: transform 0.3s ease;
}

.video-card:hover {
  transform: scale(1.05);
}

.video-card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 10px;
}
</style>